package com.test.com.bluetoothchatandroid.receiver;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.test.com.bluetoothchatandroid.callback.PairedCallback;

/**
 * Created by ${cs} on 2018/4/18.
 * @author saviorT
 *
 * 配对状态
 */

public class PairedBroadcastReceiver extends BroadcastReceiver {


	private PairedCallback pairedCallback;

	public PairedBroadcastReceiver(PairedCallback pairedCallback) {
		this.pairedCallback = pairedCallback;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
			// 取得状态改变的设备 更新设备列表
			BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
			if (device != null){
				resolveBondingState(device.getBondState());
			}
		}
	}

	private void resolveBondingState(int bondState) {
		if(pairedCallback == null){
			return;
		}

		switch (bondState){
			case BluetoothDevice.BOND_BONDED:
				pairedCallback.bonded();
				break;
			case BluetoothDevice.BOND_BONDING:
				pairedCallback.bonding();
				break;
			case BluetoothDevice.BOND_NONE:
				pairedCallback.unBonded();
				break;
			default:
				pairedCallback.bondFailed();
				break;
		}
	}


}
